﻿@using WebUI.Areas.Admin.Models.ThemeTemplateVM
@using Framework.HtmlHelpers
@using WebUI.Areas.Admin.Models.Common
@using WebUI.Extensions
@using Domain
@model ThemeTemplateListViewModel
@{
	Layout = "~/Areas/Admin/Views/Shared/_AdminContent.cshtml";
	string currentCat = ((string)ViewBag.Cat).ToLower();
	string[] cat = { "已启用", "已禁用", "已安装", "未安装" };
	string[] catLink = { "open", "close", "installed", "withoutinstalled" };
}
@section head {
}
@section body {
	<script src="~/dist/pview/pview.js"></script>
	<script>

		function showMessage(message, type) {
			$("#js-message").fadeIn();
			switch (type) {
				case -1:
					$("#js-message").attr("class", "alert alert-danger").find(".message").html(message);
					break;
				case 1:
					$("#js-message").attr("class", "alert alert-success").find(".message").html(message);
					break;
			}
			// 3s 后隐藏
			setTimeout(function () {
				$("#js-message").fadeOut("slow");
			}, 3000);
		}

		function setDefault(id) {
			$('#js-btn-default-' + id).attr('disabled', 'disabled').html('设置中');
			$.ajax({
				url: '/Admin/ThemeTemplate/SetDefault',
				type: 'post',
				data: { id: id },
				dataType: 'json',
				success: function (data) {
					if (data.code <= 0) {
						$('#js-btn-default-' + id).removeAttr('disabled').html('设置为默认');
						showMessage(data.message, -1);
					} else {
						// 成功
						$('#js-nav-tabs li[class="active"] a')[0].click();
						showMessage(data.message, 1);
					}
				}
			});
		}

		function openClose(id) {
			var text = $('#js-btn-openClose-' + id).html();
			$('#js-btn-openClose-' + id).attr('disabled', 'disabled').html(text + '中');
			$.ajax({
				url: '/Admin/ThemeTemplate/OpenClose',
				type: 'post',
				data: { id: id },
				dataType: 'json',
				success: function (data) {
					if (data.code <= 0) {
						$('#js-btn-openClose-' + id).removeAttr('disabled').html(text);
						showMessage(data.message, -1);
					} else {
						$('#js-nav-tabs li[class="active"] a')[0].click();
						showMessage(data.message, 1);
					}
				}
			});
		}

		function installZip(templateName) {
			$('#js-btn-installZip-' + templateName).attr('disabled', 'disabled').html('安装中');
			$.ajax({
				url: '/Admin/ThemeTemplate/InstallZip',
				type: 'post',
				data: { templateName: templateName },
				dataType: 'json',
				success: function (data) {
					if (data.code <= 0) {
						$('#js-btn-installZip-' + templateName).removeAttr('disabled').html('安装');
						showMessage(data.message, -1);
					} else {
						$('#js-btn-installZip-' + templateName).removeAttr('disabled').html('已安装');
						showMessage(data.message, 1);
					}
				}
			});
		}

		function uninstall(id) {
			$('#js-btn-uninstall-' + id).attr('disabled', 'disabled').html('卸载中');
			$.ajax({
				url: '/Admin/ThemeTemplate/Uninstall',
				type: 'post',
				data: { id: id },
				dataType: 'json',
				success: function (data) {
					if (data.code <= 0) {
						$('#js-btn-uninstall-' + id).removeAttr('disabled').html('卸载');
						showMessage(data.message, -1);
					} else {
						$('#js-nav-tabs li[class="active"] a')[0].click();
						showMessage(data.message, 1);
					}
				}
			});
		}

		function deleteInstallZip(templateName) {
			$('#js-btn-delete-' + templateName).attr('disabled', 'disabled').html('删除中');
			$.ajax({
				url: '/Admin/ThemeTemplate/DeleteInstallZip',
				type: 'post',
				data: { templateName: templateName },
				dataType: 'json',
				success: function (data) {
					if (data.code <= 0) {
						$('#js-btn-delete-' + templateName).removeAttr('disabled').html('删除');
						showMessage(data.message, -1);
					} else {
						$('#js-nav-tabs li[class="active"] a')[0].click();
						showMessage(data.message, 1);
					}
				}
			});
		}

		var pview;
		$(function () {
			pview = new PView();
			pview.init();
		});


	</script>
}

<!-- start 提示框 -->
@{ Html.RenderPartial("_MessagePartial"); }
<!-- end 提示框 -->
<!-- start 切换页头 -->
<div class="row" style="padding-bottom: 24px;" pview="header">
	<div class="col-md-12">
		<div class="pull-left">
			<ul id="js-nav-tabs" class="nav nav-tabs" pview-btn-group pview-targets="header,list">
				@for (int i = 0; i < cat.Length; i++)
				{
					string active = null;
					if (currentCat == catLink[i])
					{
						active = "active";
					}
					<li class="@active" role="presentation"><a href="@Url.Action("Index", new { cat = catLink[i] })">@cat[i]</a></li>
				}
			</ul>
		</div>
		<div class="pull-right col-md-4">
			<div class="btn-group">
				<a class="btn btn-primary" href="@Url.Action("UploadTemplate")">上传本地主题模板</a>
			</div>
		</div>
	</div>
</div>
<!-- end 切换页头 -->
<!-- start 模板列表 -->
<div class="row" pview="list">
	<div class="col-xs-12">
		@if (Model.List.FirstOrDefault() is OpenCloseItem)
		{
			foreach (OpenCloseItem item in Model.List)
			{
				<div class="col-sm-6 col-md-3">
					<div class="thumbnail">
						<img src="/@item.PreviewImageUrl" alt="@item.Title">
						<div class="caption">
							<h3>@item.Title</h3>
							<p><span>模板名: </span><span>@item.TemplateName</span></p>
							<p>
								<span>作者:  </span>
								@string.Join(", ", item.Authors.ToArray())
							</p>
							<p><span>描述: </span><span>@item.Description</span></p>
							<p id="js-btn-group-@item.ID" class="btn-group">
								@if (item.Status == 1)
								{
									if (item.IsDefault)
									{
										<button id="js-btn-default-@item.ID" class="btn btn-success" onclick="setDefault(@item.ID)" disabled>默认</button>
									}
									else
									{
										<button id="js-btn-default-@item.ID" class="btn btn-primary" onclick="setDefault(@item.ID)">设置为默认</button>
									}
								}
								@if (!item.IsDefault)
								{
									<button id="js-btn-openClose-@item.ID" class="btn btn-info" onclick="openClose(@item.ID)">@(item.Status == 1 ? "禁用" : "启用")</button>
									<button id="js-btn-uninstall-@item.ID" class="btn btn-danger" onclick="uninstall(@item.ID)">卸载</button>
								}
							</p>
						</div>
					</div>
				</div>
			}
		}
		else if (Model.List.FirstOrDefault() is InstallZipItem)
		{
			<div class="table-responsive no-padding">
				<table class="table table-hover">
					<tr>
						<th>文件名称</th>
						<th>文件描述</th>
						<th>版权</th>
						<th>版本</th>
						<th>安装包大小</th>
						<th>安装</th>
					</tr>
					@foreach (InstallZipItem item in Model.List)
					{
						<tr>
							<td>@item.FileName</td>
							<td>@item.FileDescription</td>
							<td>@item.LegalCopyright</td>
							<td>@item.Version</td>
							<td>@item.Length KB</td>
							<td>
								<div class="btn-group" pview-btn-group pview-targets="list">
									<button id="js-btn-installZip-@item.TemplateName" class="btn btn-primary" onclick="installZip('@item.TemplateName')">安装</button>
									<button id="js-btn-delete-@item.TemplateName" class="btn btn-danger" onclick="deleteInstallZip('@item.TemplateName')">删除</button>
								</div>
							</td>
						</tr>
					}
				</table>
			</div>
		}
	</div>
	<!-- start 分页 -->
	<div id="js-page" class="col-xs-12">
		<ul class="pagination pull-right" pview-btn-group pview-targets="header,list">
			@Html.PageLinks(Model.PageInfo, i => Url.Action("Index", new { pageIndex = i, q = Request["q"], type = Request["type"] }))
		</ul>
	</div>
	<!-- end 分页 -->
</div>
<!-- end 模板列表 -->